#include "DoubleBallGridCellRenderer.h"

#include <wx/msgdlg.h>
#include <wx/gdicmn.h>
#include <wx/log.h>
#include <wx/settings.h>
int DoubleBallGridCellRenderer::DIVIDE_COL=7;
bool DoubleBallGridCellRenderer::FILLEDBLANK=false;//产看间隔
bool DoubleBallGridCellRenderer::FILLEDDISTANCE=false;//产看行间隔
wxGridTableBase* DoubleBallGridCellRenderer:: DIRTTABLE;
    int DoubleBallGridCellRenderer::formCol=-1;//上一列
    int DoubleBallGridCellRenderer::formRow=-1;//保持行
    int DoubleBallGridCellRenderer::IndicateCol=-1;//上一列指示器
DoubleBallGridCellRenderer::DoubleBallGridCellRenderer()
{
   bestWidth=16;
   bestHeight=32;
   hasEnter=false;
   _NORMALFONT=wxNullFont ;

   if(DIRTTABLE!=NULL)
   {
    delete DIRTTABLE;
    DIRTTABLE=NULL;;
   }
   }

DoubleBallGridCellRenderer::~DoubleBallGridCellRenderer()
{
    //dtor
}

void DoubleBallGridCellRenderer::SetTextColoursAndFont(const wxGrid& grid,
                                                     const wxGridCellAttr& attr,
                                                     wxDC& dc,
                                                     bool isSelected)
{
    dc.SetBackgroundMode( wxTRANSPARENT );

    // TODO some special colours for attr.IsReadOnly() case?

    // different coloured text when the grid is disabled
    if ( grid.IsEnabled() )
    {
        if ( isSelected )
        {
            wxColour clr;
            if ( wxWindow::FindFocus() ==
                 wx_const_cast(wxGrid&, grid).GetGridWindow() )
                clr = grid.GetSelectionBackground();
            else
                clr = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW);

            dc.SetTextBackground( wxColour(192,192,222) );//clr
            dc.SetTextForeground( grid.GetSelectionForeground() );
        }
        else
        {
            dc.SetTextBackground( attr.GetBackgroundColour() );
            dc.SetTextForeground( attr.GetTextColour() );
        }
    }
    else
    {
        dc.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
        dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
    }

  //  dc.SetFont( attr.GetFont() );
}

void  DoubleBallGridCellRenderer::Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool isSelected)
{

    int   selectRow=grid.GetCursorRow();
    int selectCol =grid.GetCursorColumn();

     dc.SetFont(NORMALFONT);
  if(formRow==-1 ||(formRow!=-1 && formRow!=row)) //row change
    {
     formRow=row;
     formCol=-1;//change line
     IndicateCol=-1;
    }

  if(!hasEnter)
    {
      oldBG=attr.GetBackgroundColour();
      hasEnter=true;
    }


        wxString str = grid.GetTable()->GetValue(row,col);
   //得到数据
      wxString tmpValue;int count=0;
     if( col>1 && col<grid.GetNumberCols()-1 && row>0 && str==wxEmptyString)//FILLEDBLANK &&
        {
          for(int dec=row;dec>=0;dec--)
            {
             tmpValue= grid.GetTable()->GetValue(dec,col);
//             debug(wxString::Format(_("row,col(%02d,%02d):")+tmpValue,dec,col));
              if(tmpValue!=wxEmptyString)break;
              count++;//得到上一个数字的间隔
            }
        }
        if(count!=0)
        {
         if(FILLEDBLANK)
         {
         str=wxString::Format(_("%02d"),count);
         }
         tmpValue=wxString::Format(_("%02d"),count);
          DIRTTABLE->SetValue(row,col,tmpValue);
        }

        wxColour BgColor = attr.GetBackgroundColour();
        wxColour FgColor = attr.GetTextColour();


//增加上一行的背景
//费波那契级数 1、2、3、5、8、13、21、34、55、89、144、233

     wxString term=grid.GetTable()->GetValue(row,0);
     long intterm=0;
     term.ToLong(&intterm,10);
     //显示2,3,9期数据，往往本期数据包含在这机器里
     //菲波那契级数 2、3、5、8、13、21
     if(row==(selectRow-1)||row==(selectRow-3)
//        ||row==(selectRow-3)||row==(selectRow-5)
//        ||row==(selectRow-8)||row==(selectRow-13)
//        ||row==(selectRow-21)
        )//
     {
       BgColor= wxColour(253,245,230);
   //    FgColor=wxColour(255,FgColor.Green(),FgColor.Blue());//highlight
     }
//     else if(row==(selectRow-2)||row==(selectRow-4))
//              {
//       BgColor= wxColour(0,0,255);
//       FgColor=wxColour(255,FgColor.Green(),FgColor.Blue());//highlight
//              }
     else
     {
        BgColor= oldBG;
     }

    int realcol=col-1;
//     BgColor= oldBG;
     if(realcol%DIVIDE_COL==0)
     {
       BgColor= wxColour(192,252,192);
     }
     /*
        dc.SetBackgroundMode( wxSOLID );
        dc.SetBrush(wxBrush(BgColor, wxSOLID));
        dc.SetPen(*wxTRANSPARENT_PEN);
        dc.SetTextBackground(BgColor);
        dc.SetTextForeground(FgColor);
*/

        dc.SetTextBackground(BgColor);
        attr.SetBackgroundColour(BgColor);


 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);

    SetTextColoursAndFont(grid, attr, dc, isSelected);

    // draw the text right aligned by default
    int hAlign, vAlign;
    attr.GetAlignment(&hAlign, &vAlign);
    hAlign = wxRIGHT;

    wxRect rectCell = rect;
    //rectCell.Inflate(-1);
//得到本列每个数字之间的间隔
   wxString tmp;
   int countindex=0;
  if(selectCol==col && selectRow>0)//0行不需要了
     {
//  wxLogMessage(_("Row:%03d, Col:%03d,value:")+grid.GetTable()->GetValue(selectRow,selectCol),selectRow,selectCol);
     for(int lines=(selectRow-1);lines>0;lines--)
      {
      tmp=grid.GetTable()->GetValue(lines,col);
       if(tmp==wxEmptyString)
         {
           countindex++;
         }else
         {
             break;
         }
      }
       int another=1;
       //填充当前列和上一次出现数字之间的空白技术
      for(int lines=selectRow-countindex;lines<selectRow;lines++)
      {
          dc.SetTextForeground(wxColour(L"BLUE"));
          grid.DrawTextRectangle(dc, wxString::Format(_("%02d"),another), grid.CellToRect(lines, col), hAlign, vAlign);
            another=another+1;
      }
     }
//得到所有空行

  if(FILLEDBLANK && count!=0)//写空白
     {
    dc.SetFont(SMALLFONT);
    dc.SetTextForeground(*wxLIGHT_GREY);
     }else
     {
      dc.SetFont(_NORMALFONT);
      dc.SetTextForeground(FgColor);
     }
    grid.DrawTextRectangle(dc, str, rectCell, hAlign, vAlign);
    //画出间隔
        dc.SetFont(SMALLFONT);
       wxString value=grid.GetTable()->GetValue(row,col);

      bool isRedEnd=((col>IndicateCol && col==2)||(col<IndicateCol && col==grid.GetNumberCols()-1))?true:false;//最后一个红球位置
    //填充间隔数据
       if(FILLEDDISTANCE &&col>1)
       {

             if(value!=wxEmptyString || col==2)//
             {
               int cWidth=dc.GetCharWidth();
               unsigned PenMask = attr.GetFont().GetPointSize();


             if(col==2 && value==wxEmptyString)
                 {
                  col=1;
                 }
                wxString diff=wxEmptyString;
                int tmp=abs(col-formCol);//absolute
                if(tmp!=0)
                diff.Printf(_("%2d"),tmp);

               wxRect cRect=grid.CellToRect(row,col);
               wxRect cForm=grid.CellToRect(row,formCol);
               wxRect  cBig   =(col>IndicateCol)?cRect:cForm;
               wxRect  cSmall =(col>IndicateCol)?cForm:cRect;
               int dis=abs((cBig.GetLeft()-cSmall.GetRight()))/2;
                 dc.SetTextForeground(wxColor(0x33,0x99,0xff));
                int x=cSmall.GetRight()+(dis-(cWidth));
                int y=(cSmall.GetTop()+5);

               wxLogMessage(_("cursor key?(%02d-%02d)"),row,col);
                wxRect loc=wxRect(x,y,15,15);
 //              grid.DrawTextRectangle(dc,diff,loc,hAlign,vAlign);
                dc.DrawText(diff,x,y);
               formCol=col;
             }
       }
IndicateCol=col;//指示器
}

wxSize DoubleBallGridCellRenderer::GetBestSize(wxGrid& grid, wxGridCellAttr& attr,wxDC& dc,int row, int col)
{
   return wxSize(bestWidth, bestHeight);
}

wxGridCellRenderer * DoubleBallGridCellRenderer::Clone() const
 {
wxLogMessage(_(" clone here"));
      return new DoubleBallGridCellRenderer();
}
